package com.example.jikeyoujikeyou.office_sz.view.activity.register.subActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jikeyoujikeyou.office_sz.R;
import com.example.jikeyoujikeyou.office_sz.view.TitleBar;
import com.example.jikeyoujikeyou.office_sz.view.base.BaseActivity;

public class EditEnterpriseAddressActivity extends BaseActivity {

    private TitleBar titleBar;
    private LinearLayout auto_location;
    private EditText et_detail_location;
    private TextView tv_location;

    private static final int SUCCESS = 300;
    private static final int FAILURE = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_enterprise_address);
        init();
    }

    private void init() {
        setTitle("注册账户");
        titleBar = getTitleBar();
        titleBar.getIv_return().setVisibility(View.GONE);
        titleBar.getTv_cancel().setVisibility(View.VISIBLE);
        titleBar.getTv_setting().setVisibility(View.VISIBLE);

        titleBar.getTv_cancel().setText("取消");
        titleBar.getTv_setting().setText("保存");

        auto_location = (LinearLayout) findViewById(R.id.auto_location);
        et_detail_location = (EditText) findViewById(R.id.et_detail_location);
        tv_location = (TextView) findViewById(R.id.tv_location);

        auto_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Handler reviseEnterprise_address = new Handler() {
                    @RequiresApi(api = Build.VERSION_CODES.M)
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        tv_location.setText("深圳信息学院");
                        et_detail_location.setText("广东省深圳市龙岗区龙翔大道2188号深圳信息职业技术学院");
                        et_detail_location.setTextColor(getResources().getColor(R.color.black,null));
                    }
                };
                reviseEnterprise_address.sendMessage(reviseEnterprise_address.obtainMessage());
            }
        });

        titleBar.getTv_cancel().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(FAILURE, new Intent());
                finish();
            }
        });

        titleBar.getTv_setting().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String enterprise_address =  et_detail_location.getText().toString();
                Intent intent =  new Intent();
                intent.putExtra("enterprise_address",enterprise_address);
                if (enterprise_address == null || enterprise_address.trim().equals("")) {
                    setResult(FAILURE,intent);
                } else {
                    setResult(SUCCESS,intent);
                }
                finish();
            }
        });
    }


    private void setTitle(String title){
        getTitleBar().getTv_title().setText(title);
    }

}
